﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LeoShi.Soft.OpenSinaAPI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data;
using TCS.DBUtility;
using TCS.Model.Weibo;
using System.Xml;
using LTP.Common;
using System.Web.UI.HtmlControls;
using WeiBo.SinaAPI;

namespace TCS.Web.MembersHome
{
    public partial class ToMyWeiBo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string aa = "";
            if ((Session["BangShangMember"] != null) || (Session["BangShangMember"].ToString() != ""))
            {
                Session["WeiBoToMe"] = "http://api.t.sina.com.cn/statuses/mentions.json";//提到我的
                //BindRdoBtn();//绑定按钮
                BindWeiBoToMeList();

            }
            else
            {
                Response.Redirect("~/membersHome/login.aspx");
            }
        }
        private void BindWeiBoToMeList()
        {
            long userId = long.Parse(Session["memberid"].ToString());//帮商会员id
           string fanSql = " UserID="+userId;   //粉丝
           string follSql = " UserID=" + userId; //关注
                DataSet fans = new TCS.BLL.Weibo.WeiBo_FansInfo().GetList(fanSql);
                DataSet followers = new TCS.BLL.Weibo.WeiBo_followersInfo().GetList(follSql);
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);
            httpRequest.Token = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            string url = Session["WeiBoToMe"].ToString();
            //WeiBo.SinaAPI.SinaBase.Comment = WeiBo.SinaAPI.UserController.WeiBoCommentControlle;
            string result = httpRequest.Request(url, String.Empty);
            List<Status> myWeiboList = JSONConvert.DeserializeJsonToListObjectStatus(result);
            List<Status> myWeiboList2 = new List<Status>();

            foreach (Status status in myWeiboList)
            {

                if (rdoRlayAll.Checked)  //全部
                {
                    myWeiboList2 = myWeiboList;
                }
                if (rdoIsReplay.Checked)   // 回复
                {
                    if (status.in_reply_to_status_id != null)
                    {
                        myWeiboList2.Add(status);
                    }
                }
                if (rdoNoReplay.Checked)    //未回复
                {
                    if (status.in_reply_to_status_id ==null)
                    {
                        myWeiboList2.Add(status);
                    }
                }
            }
            List<Status> myWeiboList3 = new List<Status>();
            foreach (Status status in myWeiboList2)
            {
               
                if(rdoAllPerson.Checked)
                {
                    myWeiboList3 = myWeiboList2;
                }
                if(rdoFollwers.Checked)  //我的粉丝
                {
                    for (int i = 0; i < fans.Tables[0].Rows.Count;i++)
                    {
                        if (status.user.id == fans.Tables[0].Rows[i]["FansWBId"].ToString())  //如果我的粉丝表中有
                        {
                            myWeiboList3.Add(status);
                        }
                    }
                }
                if(rdoFirend.Checked)   // 我关注的
                {
                    for (int i = 0; i < followers.Tables[0].Rows.Count; i++)
                    {
                        if (status.user.id == followers.Tables[0].Rows[i]["FansId"].ToString())  //如果我的粉丝表中有
                        {
                            myWeiboList3.Add(status);
                        }
                    }
                }
                if(rdoVerify.Checked)    //认证用户
                {
                    if (status.user.verified != "false")
                    {
                        myWeiboList2.Add(status);
                    }
                }
                if(rdoStranger.Checked)  //陌生人
                {
                 
                }
                if (status.in_reply_to_status_id == null)//没有回复
                {

                }
            }
            List<Status> myWeiboList4 = new List<Status>();
            foreach (Status status in myWeiboList3)
            {
                int count = 0;
                if (txtCout.Text != "")
                {
                    count = int.Parse(txtCout.Text.ToString());
                }
                if (txtCout.Text.ToString() == "")
                {
                    myWeiboList4 = myWeiboList3;
                }
                else
                {
                    if (status.text.Length>count)
                    {
                        myWeiboList4.Add(status);
                    }

                }

            }
            resultCount.Text = myWeiboList4.Count.ToString();
            rptToMyWeibos.DataSource = myWeiboList4;
            rptToMyWeibos.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string itemId = "";
            foreach (RepeaterItem item in this.rptToMyWeibos.Items)
            {
                CheckBox cb = (CheckBox)item.FindControl("chkItem");
                if (cb.Checked ==false)
                {
                    long ID = Convert.ToInt64(((HtmlInputHidden)item.FindControl("hfID")).Value);
                    // 微博删除
                    string delete = "http://api.t.sina.com.cn/statuses/destroy/:id.xml";
                    try
                    {
                        itemId = ID + ",";
                    }
                    catch (System.Data.OleDb.OleDbException E)
                    {
                        throw new Exception(E.Message);
                    }
                    finally
                    {
                      
                    }
                    Response.Write("<script>alert('"+itemId+"')</script>");
                }
            }


        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindWeiBoToMeList();
        }
        //时间转换
        public string SinaDateConvert(object obj)
        {
            string result = DateTime.Now.ToString();
            result = StringHelper.SinaDataConvert(obj.ToString());
            return result;
        }
    }
}
